<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="类目名称" prop="typeTitle">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入类目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
        <el-col :span="1.5"> </el-col>
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      border
      v-loading="loading"
      :data="typeList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" type="index" width="50" />

      <el-table-column label="分类名称" align="center" prop="name" width="250">
        <!-- <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_type_type" :value="scope.row.typeType"/>
          </template> -->
      </el-table-column>
      <el-table-column label="类目名称" align="center" prop="cateName">
      </el-table-column>
      <el-table-column label="规格" align="center" prop="specName">
      </el-table-column>
      <el-table-column label="单位" align="center" prop="brandName">
      </el-table-column>
      <el-table-column label="包装" align="center" prop="packages">
      </el-table-column>
      <el-table-column label="要求" align="center" prop="demand">
      </el-table-column>
      <el-table-column label="备注" align="center" prop="other">
      </el-table-column>

      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="100"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改类目对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="580px"
      append-to-body
      class="universal-from"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        class="el-form"
      >
        <div class="disp-from-flxe">
          <el-form-item label="分类名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入" />
          </el-form-item>
        </div>
        <!-- <div class="disp-from-flxe">
          <el-form-item label="类目" prop="cateId">
            <el-select v-model="form.cateId">
              <el-option
                v-for="item in cate"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </div> -->
        <div class="disp-from-flxe">
          <el-form-item label="规格" prop="specId">
            <el-select filterable v-model="form.specId">
              <el-option
                v-for="item in specifications"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </div>
        <div class="disp-from-flxe">
          <el-form-item label="单位" prop="brandId">
            <el-select filterable v-model="form.brandId">
              <el-option
                v-for="item in brand"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </div>
        <div class="disp-from-flxe">
          <el-form-item label="包装">
            <el-input v-model="form.packages" placeholder="请输入" />
          </el-form-item>
        </div>
        <div class="disp-from-flxe">
          <el-form-item label="要求">
            <el-input v-model="form.demand" placeholder="请输入" />
          </el-form-item>
        </div>
        <div class="disp-from-flxe">
          <el-form-item label="备注">
            <el-input v-model="form.other" placeholder="请输入" />
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import {
  getFormaType,
  getType,
  delType,
  addType,
  updateType,
} from "@/api/system/type";
import { getFormatbrandData } from "@/api/system/brand";
import { getFormatCateData } from "@/api/system/cate";
import { getFormatSpecData } from "@/api/system/specifications";

export default {
  name: "type",
  dicts: ["sys_type_status", "sys_type_type"],
  data() {
    return {
      brand: [], //单位
      cate: [], //类目
      specifications: [], //规格
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 类目表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        cateId: 0,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "分类名称不能为空", trigger: "blur" },
        ],
        cateId: [
          {
            required: true,
            message: "请选择类型",
            trigger: ["blur", "change"],
          },
        ],
        specId: [
          {
            required: true,
            message: "请选择规格",
            trigger: ["blur", "change"],
          },
        ],
        brandId: [
          {
            required: true,
            message: "请选择单位",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  created() {
    this.queryParams.cateId = this.$route.params.cid;
    // console.log()
    this.getList();

    this.show();
    //   import { getFormatbrandData } from "@/api/system/brand";
    //   import { getFormatCateData } from "@/api/system/cate";
    //   import { getFormatSpecData } from "@/api/system/specifications";
  },
  methods: {
    show() {
      //单位
      getFormatbrandData({ pageNum: -1, pageSize: -1 }).then((response) => {
        this.brand = response.rows;
      });
      //类目
      getFormatCateData({ pageNum: -1, pageSize: -1 }).then((response) => {
        console.log(response);
        this.cate = response.rows;
      });
      //规格
      getFormatSpecData({ pageNum: -1, pageSize: -1 }).then((response) => {
        this.specifications = response.rows;
        console.log(this.getFormatSpecData);
      });
    },
    /** 查询类目列表 */
    getList() {
      this.loading = true;
      getFormaType(this.queryParams).then((response) => {
        this.typeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        brandId: undefined, //单位id
        cateId: this.queryParams.cateId, //行业类目id
        demand: undefined, //要求
        name: undefined, //名称
        other: undefined, //其他
        packages: undefined, //包装
        parentId: -1, //上级分类id
        specId: undefined, //规格id
        id: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.typeId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加类目";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const typeId = row.id;
      getType(typeId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改类目";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updateType(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addType(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const typeIds = row.id;
      this.$modal
        .confirm('是否确认删除类目编号为"' + typeIds + '"的数据项？')
        .then(function () {
          return delType(typeIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

    /** 导出按钮操作 */
    handleExport() {
      // http://localhost:8081/system/type/export
      this.download(
        "/system/type/export",
        {
          ...this.queryParams,
        },
        `分类导出_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
  